package zhongbo.java.air.dictionary.service.impl;

import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.exception.BusinessException;
import zhongbo.java.air.common.exception.ExceptionCode;
import zhongbo.java.air.common.exception.NoStackLogException;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.dictionary.mapper.AirCraftMapper;
import zhongbo.java.air.dictionary.model.entities.AirCraft;
import zhongbo.java.air.dictionary.service.IAirCraftService;

import java.util.Date;
import java.util.Objects;

/**
 * @author WeiJin
 * @version 1.0
 * @date 2025/3/31 10:48
 */
@Service
public class AirCraftServiceImpl implements IAirCraftService {

    @Autowired
    private AirCraftMapper airCraftMapper;

    @Override
    public AirCraft saveAirCraft(AirCraft airCraft) {
        // 参数校验
        validAirCraft(airCraft);

        // 唯一参数校验
        AirCraft dataAirCraft = queryByIcaoCode(airCraft.getIcaoCode());
        // 新增查询出来就说明icaoCode已存在
        if (Objects.nonNull(dataAirCraft)) {
            // 存在 抛出异常 icaoCode 已存在
            throw new BusinessException(ExceptionCode.ICAO_CODE_EXISTED);
        }

        // 新增时指定当前日期为创建时间和修改时间
        airCraft.setCreateTime(new Date());
        airCraft.setUpdateTime(new Date());

        int inserted = airCraftMapper.insert(airCraft);
        // 根据影响表中记录数做出相应的返回
        if (inserted > 0) {
            return airCraft;
        }
        throw new BusinessException(ExceptionCode.SAVE_FAILED);
    }

    @Override
    public String delAirCraft(String id) {
        int deleted = airCraftMapper.deleteById(id);
        // 影响数据库记录条数做出相应的返回
        if (deleted > 0) {
            return id;
        }
        throw new BusinessException(ExceptionCode.DELETE_FAILED);
    }

    @Override
    public AirCraft modifyAirCraft(AirCraft airCraft) {

        // 参数校验
        validAirCraft(airCraft);
        // 额外校验id
        if (CharSequenceUtil.isBlank(airCraft.getId())) {
            throw new BusinessException("id不能为空");
        }

        // 修改时先查询要修改的飞机id是否存在
        if (Objects.isNull(queryById(airCraft.getId()))) {
            throw new BusinessException(ExceptionCode.AIR_CRAFT_NOT_EXIST);
        }

        // 唯一参数校验
        // 数据库中 飞机信息的 icaoCode 存在且 与当前要被修改的飞机信息id不一致说明已经存在
        AirCraft dataAirCraft = queryByIcaoCode(airCraft.getIcaoCode());
        if (Objects.nonNull(dataAirCraft) && !dataAirCraft.getId().equals(airCraft.getId())) {
            // 存在 抛出异常 icaoCode 已存在
            throw new BusinessException(ExceptionCode.ICAO_CODE_EXISTED);
        }

        // 修改时给当前时间作为修改时间
        airCraft.setUpdateTime(new Date());
        int updated = airCraftMapper.updateById(airCraft);
        // 修改成功，查询出修改后的信息，返回
        if (updated > 0) {
            return queryById(airCraft.getId());
        }
        // 修改失败 抛出异常
        throw new BusinessException(ExceptionCode.UPDATE_FAILED);

    }

    @Override
    public AirCraft queryById(String id) {
        AirCraft airCraft = airCraftMapper.selectById(id);
        // 查询不到，抛出不存在异常
        if (Objects.isNull(airCraft)) {
            throw new BusinessException(ExceptionCode.CURRENT_ID_NOT_EXIST);
        }
        return airCraft;
    }

    @Override
    public PageVo<AirCraft> pageAircraft(Integer pageNumber, Integer pageSize, String airline,
                                         String aircraftType, String status, String orderBy, Boolean orderType) {
        QueryWrapper<AirCraft> wrapper = new QueryWrapper<>();
        IPage<AirCraft> page = new Page<>(pageNumber, pageSize);
        // 所属航司不为空，添加模糊查询条件
        wrapper.like(CharSequenceUtil.isNotBlank(airline), "airline", airline);
        // 飞机类型不为空，添加查询条件
        wrapper.eq(CharSequenceUtil.isNotBlank(aircraftType), "aircraft_type", aircraftType);
        // 飞机状态不为空，添加查询条件
        wrapper.eq(CharSequenceUtil.isNotBlank(status), "status", status);
        // 排序字段不为空，添加相应的排序
        if (CharSequenceUtil.isNotBlank(orderBy)) {
            // 升序
            if (Boolean.TRUE.equals(orderType)) {
                wrapper.orderByAsc(orderBy);
            } else {
                // 降序
                wrapper.orderByDesc(orderBy);
            }
        }
        // 查询
        IPage<AirCraft> airCraftPage = airCraftMapper.selectPage(page, wrapper);
        return new PageVo<>(airCraftPage);
    }

    @Override
    public AirCraft queryByIcaoCode(String code) {
        // icaoCode 构建 QueryWrapper
        LambdaQueryWrapper<AirCraft> wrapper = new LambdaQueryWrapper<AirCraft>().eq(AirCraft::getIcaoCode, code);
        return airCraftMapper.selectOne(wrapper);
    }

    private void validAirCraft(AirCraft airCraft) {
        // 非空校验
        if (Objects.isNull(airCraft)) {
            throw new NoStackLogException("请求参数为空");
        }
        if (CharSequenceUtil.isBlank(airCraft.getIcaoCode())) {
            throw new NoStackLogException("icaoCode 不能为空");
        }
        if (CharSequenceUtil.isBlank(airCraft.getRegistration())) {
            throw new NoStackLogException("registration 不能为空");
        }
        if (CharSequenceUtil.isBlank(airCraft.getAirline())) {
            throw new NoStackLogException("airline 不能为空");
        }
        if (CharSequenceUtil.isBlank(airCraft.getAircraftType())) {
            throw new NoStackLogException("aircraftType 不能为空");
        }
        if (Objects.isNull(airCraft.getStatus())) {
            throw new NoStackLogException("status 不能为空");
        }
        // 长度校验
        if (airCraft.getIcaoCode().length() > 6) {
            throw new NoStackLogException("参数icaoCode长度最长为6");
        }
        if (airCraft.getRegistration().length() > 20) {
            throw new NoStackLogException("参数registration长度最长为20");
        }
        if (airCraft.getAirline().length() > 50) {
            throw new NoStackLogException("参数airline长度最长为50");
        }
        if (airCraft.getAircraftType().length() > 30) {
            throw new NoStackLogException("参数aircraftType长度最长为30");
        }

    }
}
